﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DBUtils;

/// <summary>
/// 该页面对应着功能说明书的员工记录查询页。
/// </summary>
public partial class CheckLeave_Individual : System.Web.UI.Page
{
    DataSet dsDept;
    DataSet dsEmp;
    DataSet dsResult;
    int iDeptID;
    int iEmpID;
    /// <summary>
    /// 网页初始化时执行该方法。设置控件属性，获取员工编号信息和员工其他信息。
    /// 该过程调用 Employee.GetEmployeeInfo(iEmpID, emp) 函数，从数据库中获取员工的其他信息。
    /// 该过程还调用 Employee.GetCEOInfo(iCEOID) 函数，从数据库中获取总经理的员工编号。
    /// 该过程还调用 CheckLvReq.GetDeptList(dsDept) 函数，从数据库中获取所有部门的列表。
    /// 该过程还调用 CheckLvReq.GetDeptMember(CInt(lblDeptID.Text), dsResult) 函数，
    /// 从数据库中获取选中部门的员工列表。该过程还调用 
    /// CheckLvReq.QueryIndividHistory(iempid, txtStartTime.Text.Trim, txtEndTime.Text.Trim, dsResult) 函数，
    /// 从数据库中获取该员工该段时间内所有的请假记录。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"]!= null)//如果带参数
        {
            iEmpID = Convert.ToInt32(Request.QueryString["id"]);
            int ceo=0;

            Employee emp = new Employee();
            Employee.GetEmployeeInfo(iEmpID, emp);

            //绑定部门列表
            int ok = CheckLvReq.GetDeptList(ref dsDept);
            cmbDeptList.DataSource = dsDept.Tables[0];
            cmbDeptList.DataTextField = dsDept.Tables[0].Columns[1].ToString();
            cmbDeptList.DataValueField = dsDept.Tables[0].Columns[0].ToString();
            cmbDeptList.DataBind();

            cmbDeptList.SelectedValue = emp.DeptID.ToString();//设置部门号
            Panel1.Visible = false;
            iDeptID = Convert.ToInt32(cmbDeptList.SelectedValue);

            ok = CheckLvReq.GetDeptMember(iDeptID, ref dsEmp);
            cmbEmpList.DataSource = dsEmp.Tables[0];
            cmbEmpList.DataTextField = dsEmp.Tables[0].Columns[1].ToString();
            cmbEmpList.DataValueField = dsEmp.Tables[0].Columns[0].ToString();
            cmbEmpList.DataBind();

            cmbEmpList.SelectedValue = iEmpID.ToString();//设置员工号

            txtStartTime.Text = "2000-01-01";
            txtEndTime.Text = "2010-01-01";
            BtnSearch_Click(sender, e);

            if (Employee.GetCEOInfo(ref ceo) == 1) //判断当前用户是否是CEO
            {
                if (ceo == Convert.ToInt32(Session["EmployeeID"]))//如果当前登陆的是CEO
                {
                    Panel1.Visible = true;
                }
                else
                {
                    Panel1.Visible = false;
                }
            }
            return;
        }
        if (!IsPostBack)
        {
            int ceo = 0;
            bool isAdmin = true;//是否有权限
            if (isAdmin)//如果有权限
            {

                //绑定部门列表
                int ok = CheckLvReq.GetDeptList(ref dsDept);
                cmbDeptList.DataSource = dsDept.Tables[0];
                cmbDeptList.DataTextField = dsDept.Tables[0].Columns[1].ToString();
                cmbDeptList.DataValueField = dsDept.Tables[0].Columns[0].ToString();
                cmbDeptList.DataBind();
                if (Employee.GetCEOInfo(ref ceo) == 1) //判断当前用户是否是CEO
                {
                    if (ceo == Convert.ToInt32(Session["EmployeeID"]))//如果当前登陆的是CEO
                    {
                        Panel1.Visible = true;

                        iDeptID = Convert.ToInt32(cmbDeptList.SelectedValue);
                    }
                    else
                    {
                        Employee emp = new Employee();
                        Employee.GetEmployeeInfo(Convert.ToInt32(Session["EmployeeID"]), emp);
                        cmbDeptList.SelectedValue = emp.DeptID.ToString();//设置部门号
                        Panel1.Visible = false;
                        iDeptID = Convert.ToInt32(cmbDeptList.SelectedValue);
                    }
                    //绑定员工列表
                    ok = CheckLvReq.GetDeptMember(iDeptID, ref dsEmp);
                    cmbEmpList.DataSource = dsEmp.Tables[0];
                    cmbEmpList.DataTextField = dsEmp.Tables[0].Columns[1].ToString();
                    cmbEmpList.DataValueField = dsEmp.Tables[0].Columns[0].ToString();
                    cmbEmpList.DataBind();
                }
            }
            else//如果没有权限
            {

            }
        }
        else//如果是postback
        {
            BtnSearch_Click(sender, e);
        }
    }

    /// <summary>
    /// 当用户单击“搜索”按钮时，该方法将检索数据库中员工请假的历史记录并以 DataGrid 的形式显示返回结果。
    /// 该过程调用 
    /// SubmitLvReq.QueryHistoryReq(CInt(lblEmpID.Text), txtStartTime.Text.Trim, txtEndTime.Text.Trim, dsResult)
    /// 函数，检索数据库中员工请假的历史记录。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnSearch_Click(object sender, EventArgs e)
    {
        dsResult = new DataSet();
        iEmpID = Convert.ToInt32(cmbEmpList.SelectedValue);
        if (SubmitLvReq.QueryHistoryReq(iEmpID, txtStartTime.Text.Trim(), txtEndTime.Text.Trim(), ref dsResult) == 1)
        {
            grdResult.DataSource = dsResult.Tables[0];
            grdResult.DataBind();
            grdResult.Visible = true;
        }
        else
        {
            this.Response.Write("<script>alert('" + "查询信息失败" + "');</script>");//弹出失败信息
        }

        Panel3.Visible = false;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grdResult_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }

    /// <summary>
    /// 当用户更改要查看的员工时，该方法更新已保存的员工编号。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void cmbEmpList_SelectedIndexChanged(object sender, EventArgs e)
    {
        iEmpID = Convert.ToInt32(cmbEmpList.SelectedValue);
    }

    /// <summary>
    /// 当用户更改选中部门时，该方法更新已保存的部门编号并更新该部门的员工列表。
    /// 该过程调用 CheckLvReq.GetDeptMember(cmbDeptList.SelectedValue, dsResult) 函数，
    /// 从数据库中获取新部门的员工列表。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void cmbDeptList_SelectedIndexChanged(object sender, EventArgs e)
    {
        iDeptID = Convert.ToInt32(cmbDeptList.SelectedValue);
        //绑定员工列表
        int ok = CheckLvReq.GetDeptMember(iDeptID, ref dsEmp);
        cmbEmpList.DataSource = dsEmp.Tables[0];
        cmbEmpList.DataTextField = dsEmp.Tables[0].Columns[1].ToString();
        cmbEmpList.DataValueField = dsEmp.Tables[0].Columns[0].ToString();
        cmbEmpList.DataBind();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grdResult_SelectedIndexChanged(object sender, EventArgs e)
    {
        int i = grdResult.SelectedIndex;
        lblApproverName.Text=dsResult.Tables[0].Rows[i][10].ToString();
        lblReason.Text = dsResult.Tables[0].Rows[i][5].ToString();
        lblDenyReason.Text = dsResult.Tables[0].Rows[i][9].ToString();

        Panel3.Visible = true;
    }
}
